package controller;

import entity.Phone;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

//输出购物车信息
@WebServlet("/ListCartServlet")
public class ListCartServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        List<Phone> cart = (ArrayList<Phone>) session.getAttribute("cart");
        if(cart == null){
            out.println("购物车中没有东西");
        }
        else {
            for (Phone ph :cart){
                out.println("<h2> 型号为:"+ph.getBrand()+" 价格为："+ph.getPrice()+"</h2>");
            }
        }
    }
}
